<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 上方搜索栏 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索</legend>
            <div class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <!-- 搜索【角色名】和【权限字符】-->
                    <div class="layui-inline">
                        <label class="layui-form-label">角色名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="inputRoleName" autocomplete="off" class="layui-input" placeholder="请输入角色名"/>
                        </div>
                        <label class="layui-form-label">权限字符</label>
                        <div class="layui-input-inline">
                            <input type="text" id="inputRolePermission" autocomplete="off" class="layui-input" placeholder="请输入权限字符"/>
                        </div>
                    </div>
                    <!-- 搜索按钮 -->
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-primary layui-border-green btn-role-action" data-type="search">
                            <i class="layui-icon layui-icon-search"></i> 搜 索
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary layui-border-box btn-role-action" data-type="refresh">
                            <i class="layui-icon layui-icon-refresh"></i> 清 空
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
        <!-- 表格 -->
        <table class="layui-hide" id="table-system-role" lay-filter="table-system-role"></table>
    </div>
</div>
<!-- 【左上按钮】表格头部工具类（左上）-->
<script type="text/html" id="toolbar-table-role">
    <div class="layui-btn-container">
        <!-- 【刷新】-->
        <button class="layui-btn layui-btn-sm layuimini-btn-primary" lay-event="btn-role-refresh"><i class="fa fa-refresh"></i></button>
        <!-- 【添加】-->
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="btn-role-add"><i class="fa fa-plus"></i> 添加</button>
        <!-- 【修改状态】下拉菜单：禁用与否 -->
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="btn-role-changeStatus">修改状态 <i class="fa fa-caret-down"></i></button>
        </div>
        <!-- 【删除】 -->
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="btn-role-delete"><i class="fa fa-trash-o"></i> 删除</button>
        </div>
    </div>
</script>
<!-- 【行内按钮】表格每行末尾的操作按钮-->
<script type="text/html" id="tool-table-role">
    <a class="layui-btn layui-btn-xs" lay-event="role-edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="role-showUser">授权用户</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="role-delete">删除</a>
</script>
<!--引入JS文件-->
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            dropdown = layui.dropdown,
            table = layui.table;

        let currPage = 1                    // 定义全局变量：记录当前页码
        // 表格
        let table_role = table.render({
            elem: '#table-system-role',
            url: '/system/role/list',
            response: {statusCode: 200},
            toolbar: '#toolbar-table-role',
            defaultToolbar: ['filter', 'exports'],
            title: '用户角色表',
            height: 'full-158',
            cols: [
                [
                    {type: "checkbox", width: 50, fixed: 'left'},
                    {field: 'id', title: 'ID', width: 80, align: 'center', sort: true, fixed: 'left'},
                    {field: 'name', title: '角色名称', align: 'center', width: 150, fixed: 'left'},
                    {field: 'permission', title: '权限字符', width: 150, align: 'center'},
                    {field: 'sort', title: '显示顺序', align: 'center', sort: true},
                    {
                        field: 'status', title: '状态', align: 'center', width: 95, templet: function (d) {
                            let checkStr = "";
                            if (d.status === 1) {
                                checkStr = " checked "
                            } else if (d.status === 2) {
                                checkStr = " checked disabled "
                            }
                            return '<input type="checkbox" lay-filter="check-role-status" value="' + d.id + '" lay-skin="switch" lay-text="正常|禁用" ' + checkStr + '/>'
                        }
                    },
                    {field: 'remark', title: '备注', align: 'center'},
                    {field: 'updateTime', title: '更新时间', align: 'center'},
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#tool-table-role', width: 200}
                ]
            ],
            page: true,
            done: function (res, curr, count) {
                // 此处修复bug：删除最后一页数据后，跳转至上一页
                if (res.data.length === 0 && curr > 1) {
                    table_role.reload({page: {curr: curr - 1}})
                }
                currPage = curr
            }
        });

        // 表格的搜索加载与刷新，几个参数的定义，详见如下注释！
        let active = {
            // 搜索：从第一页开始，检索数据不变
            search: function () {
                //执行重载
                table_role.reload({
                    page: {curr: 1},            // 页码
                    where: {                    // 检索数据
                        name: $('#inputRoleName').val(),
                        permission: $('#inputRolePermission').val()
                    }
                });
            },
            // 刷新：从第一页开始，检索数据清空
            refresh: function () {
                //执行刷新
                table_role.reload({
                    page: {curr: 1},                    // 页码
                    where: {name: "", permission: ""}  // 检索数据清空
                });
                $('#inputRoleName').val('')             // 检索栏输入的数据清空
                $('#inputRolePermission').val('')
            }
        };
        $('.btn-role-action').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 【头部按钮】监听头部按钮
        table.on('toolbar(table-system-role)', function (obj) {
            let that = this;
            switch (obj.event) {
                // 【刷新】按钮
                case "btn-role-refresh":
                    table_role.reload()
                    break;
                // 【添加】角色按钮
                case "btn-role-add":
                    layer.open({
                        title: '添加角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['600px', '500px'],
                        content: '/system/role/goAdd',
                        end: function () {
                            table_role.reload();
                        }
                    });
                    break;
                // 【修改状态】下拉菜单，控制状态用
                case "btn-role-changeStatus":
                    dropdown.render({
                        elem: that,
                        show: true,         // 外部事件触发即显示
                        data: [
                            {title: '开启', id: 1, templet: '开启 <i class="fa fa-check"> </i>'},
                            {title: '禁用', id: 0, templet: '禁用 <i class="fa fa-close"> </i>'}
                        ],
                        click: function (obj) {
                            // 选中的 菜单ID 为新的status
                            let newStatus = obj.id;
                            // 读取所有选中的按钮
                            let roleIds = "";
                            let checkRoleList = table.checkStatus('table-system-role').data
                            for (let i = 0; i < checkRoleList.length; i++) {
                                roleIds += (checkRoleList[i].id + ",")
                            }
                            roleIds = roleIds.substring(0, roleIds.lastIndexOf(',')); // 去掉末尾的逗号
                            // 选中才响应
                            if (roleIds.length > 0) {
                                let actionText = newStatus === 1 ? "开启" : "禁用"
                                // 二次询问框
                                layer.confirm('确认要 ' + actionText + ' 选中的菜单吗？', {
                                    title: false,
                                    btn: ['确定', '取消'],
                                    // 按钮【确定】的回调
                                    btn1: function (index) {
                                        // 加载层
                                        let index_loading = layer.load(1, {
                                            shade: [0.2, '#fff'] //0.1透明度的白色背景
                                        });
                                        // 发起 修改角色状态 请求
                                        $.post("/system/role/changeStatus/" + newStatus, {roleIds: roleIds}, function (data) {
                                            // 关闭加载层
                                            layer.close(index_loading)
                                            if (data.code === 200) {
                                                layer.msg(data.msg, {time: 3000, icon: 1});
                                                table_role.reload();
                                            } else {
                                                layer.msg(data.msg, {time: 3000, icon: 2});
                                            }
                                        });
                                    },
                                });
                            } else {
                                layer.msg('请先选择要修改状态的角色！', {time: 2000, icon: 7});
                            }
                        }
                    });
                    break;
                // 【删除】按钮（批量删除）
                case "btn-role-delete":
                    // 获取选中的数据
                    let checkRoleList = table.checkStatus('table-system-role').data
                    if (checkRoleList.length > 0) {
                        let roleIds = "";
                        for (let i = 0; i < checkRoleList.length; i++) {
                            roleIds += (checkRoleList[i].id + ",")
                        }
                        roleIds = roleIds.substring(0, roleIds.lastIndexOf(','));
                        layer.confirm('确认要删除选中的 ' + checkRoleList.length + ' 个角色吗？', {
                            title: false,
                            btn: ['确定', '取消'],
                            closeBtn: 0,        // 不显示右上角的关闭按钮
                            // 按钮【确定】的回调
                            btn1: function (index) {
                                // 加载层
                                let index_loading = layer.load(1, {
                                    shade: [0.2, '#fff'] //0.1透明度的白色背景
                                });
                                $.post("/system/role/delete", {roleIds: roleIds}, function (data2) {
                                    // 关闭加载层
                                    layer.close(index_loading)
                                    if (data2.code === 200) {
                                        table_role.reload()
                                        layer.msg(data2.msg, {time: 3000, icon: 1});
                                    } else {
                                        layer.msg(data2.msg, {time: 3000, icon: 2});
                                    }
                                });
                            },
                        });
                    } else {
                        layer.msg('请先勾选要删除的角色！', {time: 2000, icon: 7});
                    }
                    break;
            }
        });

        // 【行操作】监听表格行末按钮
        table.on('tool(table-system-role)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                // 【编辑】按钮响应事件
                case "role-edit":
                    layer.open({
                        title: '编辑角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['600px', '500px'],
                        content: '/system/role/goEdit/' + data.id,
                        end: function () {
                            table_role.reload();
                        }
                    });
                    break;
                // 【授权用户】按钮响应事件
                case "role-showUser":
                    let user_list_index = layer.open({
                        title: '角色已授权用户列表',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['700px', '500px'],
                        content: '/system/role/goShowUser/' + data.id
                    });
                    layer.full(user_list_index);
                    break;
                // 【删除】按钮响应事件
                case "role-delete":
                    layer.confirm('确认要删除角色 <b>' + data.name + ' </b>吗？', {
                        title: false,
                        btn: ['确定', '取消'],
                        closeBtn: 0,        // 不显示右上角的关闭按钮
                        // 按钮【确定】的回调
                        btn1: function (index) {
                            // 加载层
                            let index_loading = layer.load(1, {
                                shade: [0.2, '#fff'] //0.1透明度的白色背景
                            });
                            $.post("/system/role/delete", {roleIds: data.id}, function (data2) {
                                // 关闭加载层
                                layer.close(index_loading)
                                if (data2.code === 200) {
                                    table_role.reload()
                                    layer.msg(data2.msg, {time: 3000, icon: 1});
                                } else {
                                    layer.msg(data2.msg, {time: 3000, icon: 2});
                                }
                            });
                        },
                    });
                    break;
            }
        });

        // 【行操作】修改某行的状态：监听switch开关切换事件
        form.on('switch(check-role-status)', function (data) {
            // 该值是操作之后的值
            let isChecked = data.elem.checked;
            let actionText = isChecked ? "开启" : "禁用"
            let newStatus = isChecked ? "1" : "0"
            let roleId = data.value;
            layer.confirm('确认要 <b>' + actionText + '</b> 此角色？', {
                title: false,
                btn: ['确定', '取消'],
                closeBtn: 0,        // 不显示右上角的关闭按钮
                // 按钮【确定】的回调
                btn1: function (index) {
                    // 加载层
                    let index_loading = layer.load(1, {
                        shade: [0.2, '#fff'] //0.1透明度的白色背景
                    });
                    $.post("/system/role/changeStatus/" + newStatus, {roleIds: roleId}, function (data2) {
                        // 关闭加载层
                        layer.close(index_loading)
                        if (data2.code === 200) {
                            layer.msg(data2.msg, {time: 3000, icon: 1});
                            data.elem.checked = isChecked;
                            form.render();
                        } else {
                            layer.msg(data2.msg, {time: 3000, icon: 2});
                            data.elem.checked = !isChecked;
                            form.render();
                        }
                    });
                },
                // 按钮【取消】的回调
                btn2: function (index) {
                    data.elem.checked = !isChecked;
                    form.render();
                    layer.close(index);
                }
            });
        });

    });
</script>
</body>
</html>